package yunquan.wang.modules.base.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import yunquan.wang.base.dao.DaoUtils;
import yunquan.wang.base.dao.PropertyFilter;
import yunquan.wang.base.service.BaseService;
import yunquan.wang.base.web.BaseController;
import yunquan.wang.base.web.TreePojo;
import yunquan.wang.base.web.WebConstants;
import yunquan.wang.base.web.WebHelper;
import yunquan.wang.modules.base.entity.GroupInfo;
import yunquan.wang.modules.base.entity.GroupType;
import yunquan.wang.modules.base.service.GroupInfoService;
import yunquan.wang.modules.base.service.GroupTypeService;

@Controller
@RequestMapping("/core/groupinfo")
public class GroupInfoController extends  BaseController<GroupInfo>{
	@Resource
	private GroupInfoService groupInfoService;
	@Resource
	private GroupTypeService groupTypeService;
 

	/*
	 * 默认列表显示和排序搜索查找
	 */
	@RequestMapping(value = "/list.do")
	public ModelAndView list(HttpServletRequest request, String groupTypeId) {
		ModelAndView mv = new ModelAndView();
		List<PropertyFilter> filters = WebHelper.filterRequest(WebHelper.getRequest()) ;
		Page<GroupInfo> page =null;
		if (groupTypeId != null) {
			PropertyFilter pf = new PropertyFilter("EQS_groupType.id",	groupTypeId);
			filters.add(pf);
			  page = groupInfoService.findPage(
					  WebHelper.getPage(), filters);
			mv.addObject("groupType", groupTypeService.get(groupTypeId));
			mv.addObject("groupTypeId", groupTypeId);
		}
		else
		{
			PropertyFilter pf = new PropertyFilter("EQS_groupType.id",	"1");
			filters.add(pf);
		 page = groupInfoService.findPage(  WebHelper.getPage(),filters);
			
		}
		mv.addObject(WebConstants.ATTRIBUTE_PAGE, page);
		mv.addObject("allGroupType", groupTypeService.getNotTopGroupTypes());
		mv.addObject(WebConstants.CONTROLLER_PATH, WebHelper.getControllerPath());
		return mv;
	}

	/*
	 * 默认保存方法
	 */
	@RequestMapping(value = "/save.do")
	public ModelAndView save(@Valid GroupInfo entity,
			BindingResult bindingResult) {
				String viewName="redirect:/core/groupinfo/list.do?groupTypeId="
						+ entity.getGroupType().getId();
				ModelAndView mv=baseSave(entity,bindingResult);
				mv.setViewName(viewName);
		return mv;
	}

	/*
	 * 默认查看
	 */
	@RequestMapping(value = "/view.do")
	public ModelAndView view(String id) {
		return baseView();
	}

	/*
	 * 默认输入方法
	 */
	@RequestMapping(value = "/input.do")
	public ModelAndView input(String id, String groupTypeId) {
		ModelAndView mv = new ModelAndView();
		GroupInfo entity = new GroupInfo();
		if (StringUtils.isNotBlank(id)) {
			entity = groupInfoService.get(id);
		}  
			GroupType groupType = groupTypeService.get(groupTypeId);
			entity.setGroupType(groupType);
			String parentId=groupType.getParent().getId();
		List<GroupInfo>  parentInfos=	groupInfoService.findAll(Specifications.where(DaoUtils.eq("groupType.id", parentId)));
		mv.addObject("parentInfos", parentInfos);
		mv.addObject(WebConstants.ATTRIBUTE_ENTITY, entity);
		return mv;
	}

	@RequestMapping(value = "/tree.do")
	public @ResponseBody
	List<TreePojo> getOrgTree(Integer level) {
		return groupInfoService.getgroupTree(level);
	}
 
	@RequestMapping(value = "/delete.do")
	public  ModelAndView delete(String id) {
		return baseDelete();
	}

	@Override
	public BaseService<GroupInfo> getService() {
		return groupInfoService;
	}
}
